package com.baijia.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 * @title SerializeUtil
 * @desc TODO
 * @author caoliang
 * @date 2015年11月10日
 * @version 1.0
 */
public class SerializeUtil {

    public static byte[] serialize(Object object) {
        try(ByteArrayOutputStream baos = new ByteArrayOutputStream();ObjectOutputStream oos = new ObjectOutputStream(baos)) {
            // 序列化
            oos.writeObject(object);
            byte[] bytes = baos.toByteArray();
            return bytes;
        } catch (Exception e) {
            e.printStackTrace();
        } 
        return null;
    }

    public static Object unserialize(byte[] bytes) {
        try(ByteArrayInputStream bais= new ByteArrayInputStream(bytes);ObjectInputStream ois= new ObjectInputStream(bais)) {
            // 反序列化
            Object o = ois.readObject();
            return o;
        } catch (Exception e) {
            e.printStackTrace();
        } 
        return null;
    }
}
